/*
 * MusicLabs, Copyright (C) 2008-2009 Felippe Ragghianti <felipperag@gmail.com>,
 *                                    Savio Sena <savio.sena@gmail.com>
 *                                    http://code.google.com/p/musiclabs
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

package br.com.music;

/**
 *
 */
public enum Duration {
    LONGA                   ("wwww"),
    BREVE                   ("ww"),
    SEMIBREVE               ("w"),
    MINIM                   ("h"),
    CROTCHET                ("q"), // seminima
    QUAVER                  ("i"),
    SEMIQUAVER              ("s"),
    DEMISEMIQUAVER          ("t"),
    HEMIDEMISEMIQUAVER      ("x"),
    SEMIHEMIDEMISEMIQUAVER  ("o");

    private String duration;

    /**
     * Class constructor.
     * 
     * @param textValue
     */
    Duration(String textValue) {
        duration = textValue;
    }

    /**
     * Converts to <code>{@link Float}</code>.
     *
     * @return Returns float point precision value of this duration.
     */
    public float toFloat() {
        float dur = 0f;
        char[] str = this.duration.toCharArray();
        for(Character c : str) {
            switch(c) {
                case 'w': dur += 1f; break;
                case 'h': dur += 1f/2; break;
                case 'q': dur += 1f/4; break;
                case 'i': dur += 1f/8; break;
                case 's': dur += 1f/16; break;
                case 't': dur += 1f/32; break;
                case 'x': dur += 1f/64; break;
                case 'o': dur += 1f/128; break;
            }
        }
        return dur;
    }

    /**
     * Converts duration to JFugue playable text.
     * 
     * @return Returns the duration as string.
     */
    @Override
    public String toString() {
        return this.duration;
    }

    /**
     * Get duration description as text.
     *
     * @return Duration description as stated in enumeration.
     */
    public String getDescription() {
        return super.toString();
    }
}

